import { ACCESS_TOKEN, USER_ID ,USER_INFO} from '@/store/mutation-types'
import request from '@/utils/request'
import storage from '@/utils/storage'
const expiryTime = 1 * 86400
// 登陆成功后执行
const loginSuccess = (commit, { token, userInfo }) => {
  // 过期时间30天
  // 保存tokne和userId到缓存

  storage.set(USER_ID, userInfo.userId, expiryTime)
  storage.set(USER_INFO, userInfo, expiryTime)
  storage.set(ACCESS_TOKEN, token, expiryTime)
  // 记录到store全局变量
  commit('SET_TOKEN', token)
  commit('SET_USER_INFO', userInfo)
}

const user = {
  state: {
    // 用户认证token
    // #ifdef H5
    token: storage.get(ACCESS_TOKEN)||'',
    // 用户ID
    userId: storage.get(USER_ID)||null,
    // #endif
    // #ifdef MP-WEIXIN
    token: storage.get(ACCESS_TOKEN)||'',
    // 用户ID
    userId: storage.get(USER_ID)||null,
    // #endif
    userInfo:storage.get(USER_INFO)||null,
    stat:{
      usedShareAmount:0,//已兑换份额
      shareRate:0,// 股权份额比例，需要转换成百分制
      shareAmount:0,//我的总份额
      availableShareAmount:0,// 可兑换份额
      lockedShareAmount:0,// 锁仓份额
    }
  },

  mutations: {
    UPDATE_USER_NAME_AVATAR: (state, { userName, avatar,provinceId,cityId }) => {
      state.userInfo.nickName = userName;
      state.userInfo.avatarUrl = avatar;
      state.userInfo.provinceId = provinceId;
      state.userInfo.cityId = cityId;
      let oldUserInfo = storage.get(USER_INFO)
        oldUserInfo.nickName = userName;
        oldUserInfo.avatarUrl = avatar;
        oldUserInfo.provinceId = provinceId;
        oldUserInfo.cityId = cityId;
      storage.set(USER_INFO, oldUserInfo, expiryTime)
    },
    SET_TOKEN: (state, value) => {
      state.token = value
    },
    SET_USER_INFO: (state, value) => {
      state.userInfo = value
      state.userId = value.userId
    },
    SET_USER_STAT: (state, value) => {
      state.stat = value
    }
  },

  actions: {

    // 用户登录(普通登录: 输入手机号和验证码)
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        request.post('passport/login', { form: data })
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },

    // 微信小程序一键授权登录(获取用户基本信息)
    LoginMpWx({ commit }, data) {
      return new Promise((resolve, reject) => {
        request.post('wx/auth/login_by_weixin', data, { isPrompt: false })
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },

    // 微信小程序一键授权登录(授权手机号)
    LoginMpWxMobile({ commit }, data) {
      return new Promise((resolve, reject) => {
        request.post('passport/loginMpWxMobile', { form: data }, { isPrompt: false })
          .then(response => {
            const result = response.data
            loginSuccess(commit, result)
            resolve(response)
          })
          .catch(reject)
      })
    },

    // 退出登录
    Logout({ commit }, data) {
      const store = this
      return new Promise((resolve, reject) => {
        if (store.getters.userId > 0) {
          // 删除缓存中的tokne和userId
          storage.remove(USER_ID)
          storage.remove(ACCESS_TOKEN)
          // 记录到store全局变量
          commit('SET_TOKEN', '')
          commit('SET_USER_ID', null)
          resolve()
        }
      })
    }

  }
}

export default user
